  package com.smartwebx.ha.web.gis.module.screen;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import com.smartwebx.ha.biz.rainwater.feature.FeatureRsvrManager;
import com.smartwebx.ha.biz.rainwater.real.RsvrManager;
import com.smartwebx.ha.biz.rainwater.real.RsvrfsrManager;
import com.smartwebx.ha.dal.annot.JsonModule;
import com.smartwebx.ha.dal.annot.JsonModules;
import com.smartwebx.ha.dal.usebean.DateValue;
import com.smartwebx.ha.dal.usebean.RsvrFlood;
import com.smartwebx.ha.dal.usebean.view.ViewDayProcessAnalysis;
import com.smartwebx.ha.dal.usebean.view.ViewProcessBean;
import com.smartwebx.ha.web.util.json.module.DateModule;
import com.smartwebx.ha.web.util.json.module.DoubleModule;

public class ReservoirScreen extends BasicScreen {
	@Autowired
	private RsvrManager rsvrManager;
	@Autowired
	private RsvrfsrManager rsvrfsrManager;
	@Autowired
	private FeatureRsvrManager featureRsvrManager;

	/**
	 * 水库单站日水位过程线
	 * 
	 * @param stcd
	 * @param bTm
	 * @param eTm
	 * @param dxn
	 *            典型年2010
	 * @return
	 * @throws ParseException
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	@JsonModules({ @JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd"),
		@JsonModule(clazz = DoubleModule.class, value = "2") })
	public List<ViewProcessBean> getSingleStatDayProcess(String stcd, Date bTm,
			Date eTm, Integer dxn) throws Exception {
		// 水库水位过程
		List<DateValue> dateValueWaters = rsvrManager
				.getSingleStatWaterDayProcess(stcd, bTm, eTm);
		// 汛限水位过程
		List<DateValue> dateValueFloods = rsvrfsrManager.getSingleFloodProcess(
				bTm, eTm, stcd);
		// 日流量过程
		List<DateValue> dateValueFlows = rsvrManager
				.getSingleStatFlowDayProcess(stcd, bTm, eTm);
		// 多年同期
		List<DateValue> dateValueYears = featureRsvrManager.getPeriodContrast(
				stcd, bTm, eTm);
		// 典型年
		Calendar cal = Calendar.getInstance();
		cal.setTime(bTm);
		int begYear = cal.get(Calendar.YEAR);

		cal.setTime(eTm);
		int endYear = cal.get(Calendar.YEAR);

		int b = dxn - (endYear - begYear);

		Date startDate = DateUtils.setYears(bTm, b);
		Date endDate = DateUtils.setYears(eTm, dxn);
		List<DateValue> dxnDateValues = rsvrManager
				.getSingleStatWaterDayProcess(stcd, startDate, endDate);
		List<ViewProcessBean> viewProcessBeans = new ArrayList<ViewProcessBean>();

		for (DateValue dateValueWater : dateValueWaters) {
			// 当前水位
			ViewProcessBean viewProcessBean = new ViewProcessBean();
			BeanUtils.copyProperties(viewProcessBean, dateValueWater);
			/*viewProcessBean.setDate(dateValueWater.getTm());
			viewProcessBean.setValue(dateValueWater.getVal());*/
			// 汛限水位
			for (DateValue dateValueFlood : dateValueFloods) {
				if (Objects.equal(dateValueWater.getTm(),
						DateUtils.addHours(dateValueFlood.getTm(), 8))) {
					viewProcessBean.setTotalVal(dateValueFlood.getVal());
					break;
				}
			}
			// 流量过程
			for (DateValue dateValueFlow : dateValueFlows) {
				if (Objects
						.equal(dateValueWater.getTm(), dateValueFlow.getTm())) {
					viewProcessBean.setValue5(dateValueFlow.getVal());
					break;
				}
			}
			// 多年同期
			for (DateValue dateValueYear : dateValueYears) {
				if (Objects
						.equal(dateValueWater.getTm(), DateUtils.addHours(dateValueYear.getTm(), 8))) {
					viewProcessBean.setValue1(dateValueYear.getVal());
					break;
				}
			}
			// 典型年
			for (DateValue dxnDateValue : dxnDateValues) {
				String s1 = new SimpleDateFormat("MM-dd").format(dxnDateValue
						.getTm());
				String s2 = new SimpleDateFormat("MM-dd").format(dateValueWater
						.getTm());
				if (s1.equals(s2)) {
					viewProcessBean.setValue6(dxnDateValue.getVal());
					break;
				}
			}
			viewProcessBeans.add(viewProcessBean);
		}
		return viewProcessBeans;

	}
	
	@JsonModule(clazz = DoubleModule.class, value = "2",isNumber = true)
	public Object getSingleStatDayProcessByGisJs(String stcd, Date bTm,
			Date eTm, Integer dxn) throws Exception {
		// 水库水位过程
		List<DateValue> dateValueWaters = rsvrManager
				.getSingleStatWaterDayProcess(stcd, bTm, eTm);
		// 汛限水位过程
		List<DateValue> dateValueFloods = rsvrfsrManager.getSingleFloodProcess(
				bTm, eTm, stcd);
		// 日流量过程
		List<DateValue> dateValueFlows = rsvrManager
				.getSingleStatFlowDayProcess(stcd, bTm, eTm);
		// 多年同期
		List<DateValue> dateValueYears = featureRsvrManager.getPeriodContrast(
				stcd, bTm, eTm);
		// 典型年
		Calendar cal = Calendar.getInstance();
		cal.setTime(bTm);
		int begYear = cal.get(Calendar.YEAR);

		cal.setTime(eTm);
		int endYear = cal.get(Calendar.YEAR);

		int b = dxn - (endYear - begYear);

		Date startDate = DateUtils.setYears(bTm, b);
		Date endDate = DateUtils.setYears(eTm, dxn);
		List<DateValue> dxnDateValues = rsvrManager
				.getSingleStatWaterDayProcess(stcd, startDate, endDate);
		List<Double> vals=Lists.newArrayList();//水位
		List<Double> totalVals=Lists.newArrayList();//限讯水位
		List<Double> values5=Lists.newArrayList();//流量过程
		List<Double> values1=Lists.newArrayList();//多年同期
		List<Double> values6=Lists.newArrayList();//典型年
		for (DateValue dateValueWater : dateValueWaters) {
			// 当前水位
			vals.add(dateValueWater.getVal());
			// 汛限水位
			for (DateValue dateValueFlood : dateValueFloods) {
				if (Objects.equal(dateValueWater.getTm(),
						DateUtils.addHours(dateValueFlood.getTm(), 8))) {
					totalVals.add(dateValueFlood.getVal());
					break;
				}
			}
			// 流量过程
			for (DateValue dateValueFlow : dateValueFlows) {
				if (Objects
						.equal(dateValueWater.getTm(), dateValueFlow.getTm())) {
					values5.add(dateValueFlow.getVal());
					break;
				}
			}
			// 多年同期
			for (DateValue dateValueYear : dateValueYears) {
				if (Objects
						.equal(dateValueWater.getTm(), DateUtils.addHours(dateValueYear.getTm(), 8))) {
					values1.add(dateValueYear.getVal());
					break;
				}
			}
			// 典型年
			for (DateValue dxnDateValue : dxnDateValues) {
				String s1 = new SimpleDateFormat("MM-dd").format(dxnDateValue
						.getTm());
				String s2 = new SimpleDateFormat("MM-dd").format(dateValueWater
						.getTm());
				if (s1.equals(s2)) {
					values6.add(dxnDateValue.getVal());
					break;
				}
			}
		}
		Map<String, List<Double>> result = new HashMap<String, List<Double>>();
		result.put("vals", vals);
		result.put("totalVals", totalVals);
		result.put("values1", values1);
		result.put("values5", values5);
		result.put("values6", values6);
		return result;

	}

	/**
	 * 水库小时过程
	 * 
	 * @param stcd
	 * @param bTm
	 * @param eTm
	 * @return
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @throws ParseException
	 */
	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd HH:mm")
	public List<ViewProcessBean> getSingleStatHourProcess(String stcd,
			Date bTm, Date eTm) throws IllegalAccessException, InvocationTargetException {
		List<DateValue> dateValueWaters = rsvrManager
				.getSingleStatWaterHourProcess(stcd, bTm, eTm);// 水库时水位过程
		// 日流量过程
		List<DateValue> dateValueFlows = rsvrManager.getPurenessFlowProcesss(
				stcd, bTm, eTm);
		List<ViewProcessBean> viewProcessBeans = new ArrayList<ViewProcessBean>();
		for (DateValue dateValueWater : dateValueWaters) {
			ViewProcessBean viewProcessBean = new ViewProcessBean();
			viewProcessBean.setValue2(0.0);
			BeanUtils.copyProperties(viewProcessBean, dateValueWater);
			/*viewProcessBean.setDate(dateValueWater.getTm());
			viewProcessBean.setValue(dateValueWater.getVal());*/
			for (DateValue dateValueFlow : dateValueFlows) {
				if (Objects
						.equal(dateValueWater.getTm(), dateValueFlow.getTm())) {
					viewProcessBean.setValue2(dateValueFlow.getVal()==null ?0.0:dateValueFlow.getVal());
					break;
				}
			}
			viewProcessBeans.add(viewProcessBean);
		}
		return viewProcessBeans;

	}
	
	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd HH:mm")
	public Object getSingleStatHourProcessByGisJs(String stcd,
			Date bTm, Date eTm) throws IllegalAccessException, InvocationTargetException {
		List<DateValue> dateValueWaters = rsvrManager
				.getSingleStatWaterHourProcess(stcd, bTm, eTm);// 水库时水位过程
		// 日流量过程
		List<DateValue> dateValueFlows = rsvrManager.getSingleStatFlowHourProcess(
				stcd, bTm, eTm);
		List<Double> vals=Lists.newArrayList();//水位
		List<Double> values2=Lists.newArrayList();//流量
		for (DateValue dateValueWater : dateValueWaters) {
			vals.add(dateValueWater.getVal());
			for (DateValue dateValueFlow : dateValueFlows) {
				if (Objects
						.equal(dateValueWater.getTm(), dateValueFlow.getTm())) {
					values2.add(dateValueFlow.getVal()==null ?0.0:dateValueFlow.getVal());
					break;
				}
			}
		}
		Map<String, List<Double>> result = new HashMap<String, List<Double>>();
		result.put("vals", vals);
		result.put("values2", values2);
		return result;
	}

	/**
	 * 水库日过程特征分析
	 * 
	 * @param stcd
	 * @param bTm
	 * @param eTm
	 * @return
	 * @throws ParseException
	 */
	@JsonModules({ @JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd"),
		@JsonModule(clazz = DoubleModule.class, value = "2") })
	public ViewDayProcessAnalysis getDayProcessAnalysis(String stcd, Date bTm,
			Date eTm) {
		ViewDayProcessAnalysis viewDayProcessAnalysis = rsvrManager
				.getDayProcessAnalysis(stcd, bTm, eTm);
		return viewDayProcessAnalysis;

	}

	/**
	 * 库（湖）站防洪指标
	 * 
	 * @param stcd
	 * @return
	 */
	@JsonModule(clazz = DateModule.class, value = "yyyy-MM-dd")
	public RsvrFlood getRSVRFCCH(String stcd) {
		RsvrFlood rsvrFlood = rsvrManager.getRsvrFlood(stcd);
		if(rsvrFlood==null){
			return new RsvrFlood();
		}else{
			return rsvrFlood;
		}
	}
}
